#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：zhcx-service-test 
@File    ：baseAPI.py
@Author  ：关依林
@Date    ：2023/9/25 15:22 
'''
import json

from testcases.ProductGrading_Strategy.AmazonListing.service.listing_grading_public_way import *

def ProductDataFiltering(strategyRulePack, strategyRuleValue, productStrategyRule):
    cursorProduct = connectMysql("Java_strategyProduct")
    cursorCategory = connectMysql("Java_strategyCategory")
    minId, maxId = getMinMaxId(cursorProduct)
    tempId = minId
    matchingSkuId = []
    filedType, filedTypeOther, filedTypeRelation = strategyRulePack
    strategyRuleValue, strategyRuleValueOther,strategyRuleValueRelation = strategyRuleValue
    for id in range(minId,maxId,1000):
        startId, endId = tempId, tempId + 1000
        print(str(startId) + "-" + str(endId))
        tempId = endId
        skuData = getPaginationBaseData(startId, endId, filedType, cursorProduct)
        if filedType and skuData:
            for condition in strategyRuleValue:
                if not skuData: break
                conditionName = condition["condition_field"]
                value = json.loads(condition["condition_value"])
                if conditionName == "product_state":
                    skuData = scopeInquire7(conditionName, skuData, value=value["value"])
                elif conditionName == "product_property":
                    skuData = scopeInquire7(conditionName, skuData, value=value["value"])
                elif conditionName == "develop_type":
                    skuData = scopeInquire7(conditionName, skuData, value=value["value"])
                elif conditionName == "has_law_firm_agent":
                    value = value["value"]
                    if value == 0: value = [-1, 0]
                    skuData = scopeInquire7(conditionName, skuData, value=value)
                elif conditionName == "product_logistics_attribute":
                    skuData = scopeInquire7(conditionName="with_battery", skuData=skuData, value=value["value"])
                elif conditionName in "sku_type":
                    skuData = scopeInquire7(conditionName="is_group", skuData=skuData, value=[value["value"]])
                elif conditionName in "plug_relate_type":
                    isMark = True
                    if not bool(value["value"]):
                        isMark = False
                        value = [i for i in range(12)]
                    else:
                        value = value["plugRelateType"]
                    skuData = scopeInquire7(conditionName, skuData, value,isMark)
                elif conditionName in "last_buy_price":
                    skuData = scopeInquire(conditionName, skuData, value)
                elif conditionName in "min_sale_num":
                    skuData = scopeInquire(conditionName, skuData, value)
                elif conditionName in "single_length":
                    skuData = scopeInquire2(skuData, value)
                elif conditionName in "sum_length":
                    skuData = scopeInquire3(skuData, value)
                elif conditionName in "pack_weight":
                    skuData = scopeInquire4(skuData, value)
                elif conditionName in "volume_weight":
                    customParam = value["customParam"]
                    skuData = scopeInquire5(customParam, skuData, value)
                elif conditionName in "toss_rate":
                    skuData = scopeInquire6(customParam, skuData, value)
            skuData = set([i["sku"] for i in skuData])

        if filedTypeOther and skuData:
            conditionName = "category_id"
            value = json.loads(strategyRuleValueOther[0]["condition_value"])["value"]
            categoryData = skuCategoryInquire(cursorCategory, skuData)
            skuData = set([i["sku"] for i in scopeInquire7(conditionName, categoryData, value)])

        if filedTypeRelation and skuData:
            for condition in strategyRuleValueRelation:
                conditionName = condition["condition_field"]
                value = json.loads(condition["condition_value"])
                if conditionName == "product_activity":
                    saleStatusData =  productSaleStatusInquire(cursorProduct, skuData)
                    conditionName = "product_vitality_type"
                    skuData = set([i["sku"] for i in scopeInquire7(conditionName, saleStatusData, value=value["value"])])
                    if not skuData: break
                else:
                    value["unit"] = condition["unit"]
                    checkTimeData = productCheckTimeInquire(cursorProduct, skuData)
                    skuData = set(i["sku"] for i in scopeInquire8(conditionName, checkTimeData, value))
        matchingSkuId += list(skuData)
    return matchingSkuId